<template>
  <div class="app-container">
    <div class="filter-container">
      <el-button class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">
        查询
      </el-button>
      <div style="display:inline-block;">
        <router-link :to="{path:`/gen/config/add`}">
          <el-button
            v-if="isP('GenConfigAdd')"
            class="filter-item light-primary-btn"
            style="margin-left: 10px;"
            type="primary"
            icon="el-icon-plus"
          >
            新增
          </el-button>
        </router-link>
      </div>
    </div>
    <el-table
      v-loading="listLoading"
      :data="list"
      border
      fit
      highlight-current-row
      style="width: 100%;"
    >
      <el-table-column
        type="index"
        width="50"
      />
      <el-table-column
        prop="authorName"
        label="姓名"
        width="100"
      />
      <el-table-column
        prop="javaApiFilepath"
        label="Java-api文件路径"
        width="240"
      />
      <el-table-column
        prop="javaBizFilepath"
        label="Java-biz文件路径"
        width="240"
      />
      <el-table-column
        prop="vueFilepath"
        label="Vue文件路径"
        width="240"
      />
      <el-table-column
        prop="codemodelFilepath"
        label="代码模板的路径"
        width="240"
      />
      <el-table-column
        prop="databaseType"
        label="数据库类型"
        width="120"
        :formatter="databaseTypeFormatter"
      />
      <el-table-column align="center" label="操作" min-width="160">
        <template slot-scope="scope">
          <el-button
            v-if="isP('GenConfigUpdate')"
            style="margin-left: 10px"
            type="primary"
            size="mini"
            @click="handleUpdate(scope)"
          >修改
          </el-button>
          <el-button
            v-if="isP('GenConfigDelete')"
            style="margin-left: 10px"
            type="danger"
            size="mini"
            @click="handleDelete(scope)"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
import { listGenConfig, deleteGenConfig } from '@/api/gen/config'
import msgUtil from '@/utils/msgUtil'
import PageUtil from '@/utils/PageUtil'
import ComboConst from '@/utils/ComboConst'

export default {
  data() {
    return {
      tableKey: 0,
      list: null,
      total: 0,
      listLoading: true,
      listQuery: JSON.parse(localStorage.getItem('GenConfigQuery')) || {}
    }
  },
  created() {
    this.listGenConfig()
  },
  methods: {
    databaseTypeFormatter(row) {
      return PageUtil.formatValue(ComboConst.databaseTypeOptions, row.databaseType)
    },
    listGenConfig() {
      this.listLoading = true
      listGenConfig(this.listQuery).then(response => {
        this.list = response.data
        setTimeout(() => {
          this.listLoading = false
        }, 500)
      })
      localStorage.setItem('GenConfigQuery', JSON.stringify(this.listQuery))
    },
    // 按钮查询
    handleFilter() {
      this.listQuery.page = 1
      this.listGenConfig()
    },
    // 删除
    handleDelete({ $index, row }) {
      msgUtil.confirm('确认删除配置?').then(async() => {
        await deleteGenConfig({ id: row.id })
        msgUtil.success2('删除成功!')
        this.listGenConfig()
      })
    },
    // 处理修改
    handleUpdate({ $index, row }) {
      this.$router.push({
        path: '/gen/config/update',
        query: { id: row.id }
      })
    }
  }
}
</script>
